package com.mediacloud.app.newsmodule.fragment.video.vod

import android.graphics.Rect
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.kt.TypeX
import com.mediacloud.app.newsmodule.fragment.InfoFlowAdapter

import com.mediacloud.app.newsmodule.fragment.InfoFlowFragment
import java.util.concurrent.atomic.AtomicInteger


/**
 * float比较两个值相等
 *
 * @param num1
 * @param num2
 * @return
 */
fun VideoListPlayerUtil.isFloatEquals(num1: Float, num2: Float): Boolean {
    return Math.abs(num1 - num2) < 1e-6
}

/**
 * @param view
 * @return 当前视图可见比列
 */
fun VideoListPlayerUtil.getViewVisiblePercent(view: View?): Float {
    if (view == null) {
        return 0f
    }
    val height = view.height.toFloat()
    val rect = Rect()
    if (!view.getLocalVisibleRect(rect)) {
        return 0f
    }
    val visibleHeight = (rect.bottom - rect.top).toFloat()
    return visibleHeight / height
}

/**
 * @param firstVisiblePosition 首个可见item位置
 * @param lastVisiblePosition  最后一个可见item位置
 * @param percent              当item被遮挡percent/1时释放,percent取值0-1
 */
fun VideoListPlayerUtil.onScrollReleaseAllVideos(
    firstVisiblePosition: Int,
    lastVisiblePosition: Int,
    percent: Float
) {
    try {
        if (mAutoCurPos >= 0) {
            if (mAutoCurPos <= firstVisiblePosition || mAutoCurPos >= lastVisiblePosition - 1) {

                if (container != null) {
                    if (getViewVisiblePercent(container) < percent) {

                        stopPlay()
                        return
                    }
                }
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

/**
 * @param firstVisiblePosition 首个可见item位置
 * @param lastVisiblePosition  最后一个可见item位置
 */
//记录触发 需要去计算是否播放的次数
var atomicInteger = AtomicInteger(0)

//记录当前在可以播放的 发生在第几次触发 用于一页中同时出现两个视频都满足了播放条件
var scrollPlayCount = 0
fun VideoListPlayerUtil.onScrollPlayVideo(firstVisiblePosition: Int, lastVisiblePosition: Int) {
    atomicInteger.getAndAdd(1)
    try {
        val size = lastVisiblePosition - firstVisiblePosition
        for (i in 0..size) {
            val tempPosition = i + firstVisiblePosition
            if (mAutoCurPos == tempPosition && isPlayingOrPause) {
                return
            }
            if (mAutoCurPos != tempPosition) {
                var tempArticleItem: ArticleItem? = null
                if (hqyNavFragment != null && mRecyclerAdapter != null) {
//                    if (mRecyclerAdapter is InfoFlowAdapter) {
//                        val viewHolder: RecyclerView.ViewHolder? = recyclerView?.findViewHolderForAdapterPosition(tempPosition)
//                        if (viewHolder is InfoFlowAdapter.BigViewHolder) {
//                            if (tempPosition == lastVisiblePosition) return
//                            val item1: Any = (mRecyclerAdapter as InfoFlowAdapter).articleItems[tempPosition]
//                            item1.let {
//                                if (it is ArticleItem) {
//                                    tempArticleItem = it
//                                }
//                            }
//                            tempArticleItem?.let { tempArticleItem ->
//                                if (scrollPlayCount != atomicInteger.get() && isFloatEquals(getViewVisiblePercent(viewHolder.itemView), 1f)) {
//                                    scrollPlayCount = atomicInteger.get()
//                                    if (tempArticleItem.type == TypeX.NEWS.VIDEO || tempArticleItem.type == TypeX.NEWS.LIVE_VIDEO) {
//                                        if (hqyNavFragment is InfoFlowFragment) {
//                                            (hqyNavFragment as InfoFlowFragment).onPlayIcoClick(
//                                                viewHolder.itemView,
//                                                viewHolder.logoContainer,
//                                                viewHolder.logoLayout,
//                                                tempArticleItem
//                                            )
//                                            mAutoCurPos = tempPosition
//                                        }
//                                    }
//                                }
//                            }
//                        }
//
//                    }
//                    else if (mRecyclerAdapter is InfoFlowAdapterB) {
//                        val viewHolder: RecyclerView.ViewHolder? =
//                            recyclerView?.findViewHolderForAdapterPosition(tempPosition)
//                        if (viewHolder != null && viewHolder is InfoFlowAdapterB.BigViewHolder) {
//
//                            var item2: Any? =
//                                (mRecyclerAdapter as InfoFlowAdapterB).articleItems[tempPosition]
//                            item2?.let {
//                                if (it is ArticleItem) {
//                                    tempArticleItem = it
//                                }
//                            }
//                            tempArticleItem?.let { tempArticleItem->
//                                if (tempArticleItem.type== TypeX.NEWS.VIDEO
//                                    ||tempArticleItem.type== TypeX.NEWS.LIVE_VIDEO) {
//                                    if (isFloatEquals(getViewVisiblePercent(viewHolder.itemView), 1f)) {
//                                        if (currentFragment is InfoFlowFragment) {
//                                            (currentFragment as InfoFlowFragment).onPlayIcoClick(
//                                                viewHolder.itemView,
//                                                viewHolder.logoContainer,
//                                                tempArticleItem
//                                            )
//                                            mAutoCurPos = tempPosition
//                                        }
//                                    }
//                                }
//                            }
//
//                        }
//                    }
                }
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

}